package com.hk.controller;

import cn.hutool.json.JSONUtil;
import com.hk.Enume.OperationType;
import com.hk.annotation.AotoFile;
import com.hk.constant.LogConstaant;
import com.hk.core.PageResult;
import com.hk.core.Result;
import com.hk.domain.User;
import com.hk.dto.UserDto;
import com.hk.dto.useranddeptdto.UserDTO;
import com.hk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/system")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /** 用户列表的条件分页查询 */
    @GetMapping("/user/list")
    public PageResult list(UserDto userDto) {
        log.info("接收到用户查询请求: {}", userDto);

        // 提取 beginTime 和 endTime
        String beginTime = null;
        String endTime = null;

        if (userDto.getParams() != null) {
            beginTime = (String) userDto.getParams().get("beginTime");
            endTime = (String) userDto.getParams().get("endTime");
        }

        // 调用 service 查询
        PageResult pageResult = userService.list(
                userDto.getPageNum(),
                userDto.getPageSize(),
                userDto.getUserName(),
                userDto.getPhonenumber(),
                userDto.getStatus(),
                beginTime,
                endTime
        );

        return pageResult;
    }


    /**   根据id查询用户 */
    @GetMapping("/user/{userId}")
    public Result getUserById(@PathVariable Integer userId) {
        log.info("查询用户{}", userId);
        User user = userService.getUserById(userId);
        return Result.success(user);
    }

    /**   新增用户  */
    @AotoFile(value = OperationType.POST,title = LogConstaant.USER_LOG)
    @PostMapping("/user")
    public Result addUser(@RequestBody User user) {
        log.info("添加用户{}", user);
        userService.addUser(user);
        return Result.success();
    }

    /**   根据id删除用户   */
    @AotoFile(value = OperationType.DELETE,title = LogConstaant.USER_LOG)
    @DeleteMapping("/user/{userId}")
    public Result deleteById(@PathVariable Integer userId) {
        log.info("删除用户{}", userId);
        userService.deleteById(userId);
        return Result.success();
    }

    /**   根据id修改用户信息   */
    @AotoFile(value = OperationType.PUT,title = LogConstaant.USER_LOG)
    @PutMapping("/user")
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户{}", user);
        userService.updateUser(user);
        return Result.success();
    }

    String s = "{\n" +
            "    \"msg\": \"操作成功\",\n" +
            "    \"code\": 200,\n" +
            "    \"roles\": [\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2025-06-04 16:06:01\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 127,\n" +
            "            \"roleName\": \"G123Bond1\",\n" +
            "            \"roleKey\": \"smallCup\",\n" +
            "            \"roleSort\": \"0\",\n" +
            "            \"dataScope\": \"1\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2025-06-05 10:32:30\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 131,\n" +
            "            \"roleName\": \"AAA\",\n" +
            "            \"roleKey\": \"123c\",\n" +
            "            \"roleSort\": \"0\",\n" +
            "            \"dataScope\": \"1\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2025-06-05 16:49:07\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 133,\n" +
            "            \"roleName\": \"多少度\",\n" +
            "            \"roleKey\": \"dd\",\n" +
            "            \"roleSort\": \"0\",\n" +
            "            \"dataScope\": \"1\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"1\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-04-15 09:23:12\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 101,\n" +
            "            \"roleName\": \"市场专员\",\n" +
            "            \"roleKey\": \"marketing_specialist\",\n" +
            "            \"roleSort\": \"2\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": false,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-04-15 09:23:54\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 102,\n" +
            "            \"roleName\": \"销售专员-商机\",\n" +
            "            \"roleKey\": \"sales_specialist\",\n" +
            "            \"roleSort\": \"3\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-05-24 02:44:39\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 106,\n" +
            "            \"roleName\": \"销售主管\",\n" +
            "            \"roleKey\": \"sales_executive\",\n" +
            "            \"roleSort\": \"4\",\n" +
            "            \"dataScope\": \"4\",\n" +
            "            \"menuCheckStrictly\": false,\n" +
            "            \"deptCheckStrictly\": false,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-05-24 02:46:14\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 107,\n" +
            "            \"roleName\": \"财务主管\",\n" +
            "            \"roleKey\": \"treasurer\",\n" +
            "            \"roleSort\": \"5\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-05-19 01:32:30\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 104,\n" +
            "            \"roleName\": \"总经理\",\n" +
            "            \"roleKey\": \"general_manager\",\n" +
            "            \"roleSort\": \"6\",\n" +
            "            \"dataScope\": \"4\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-11-22 09:41:18\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 115,\n" +
            "            \"roleName\": \"线索对接员\",\n" +
            "            \"roleKey\": \"XSDJ_TEST\",\n" +
            "            \"roleSort\": \"114\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-11-22 09:42:43\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 116,\n" +
            "            \"roleName\": \"商机对接员\",\n" +
            "            \"roleKey\": \"SJDJ_TEST\",\n" +
            "            \"roleSort\": \"116\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-11-23 16:22:27\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 118,\n" +
            "            \"roleName\": \"销售部线索专员\",\n" +
            "            \"roleKey\": \"XSB_XSZY\",\n" +
            "            \"roleSort\": \"118\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-11-23 16:36:23\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 119,\n" +
            "            \"roleName\": \"销售部商机专员\",\n" +
            "            \"roleKey\": \"XSB_SJZY\",\n" +
            "            \"roleSort\": \"119\",\n" +
            "            \"dataScope\": \"5\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"createBy\": null,\n" +
            "            \"createTime\": \"2021-11-23 16:14:05\",\n" +
            "            \"updateBy\": null,\n" +
            "            \"updateTime\": null,\n" +
            "            \"roleId\": 117,\n" +
            "            \"roleName\": \"销售部主管\",\n" +
            "            \"roleKey\": \"XSBZG\",\n" +
            "            \"roleSort\": \"120\",\n" +
            "            \"dataScope\": \"4\",\n" +
            "            \"menuCheckStrictly\": true,\n" +
            "            \"deptCheckStrictly\": true,\n" +
            "            \"status\": \"0\",\n" +
            "            \"delFlag\": \"0\",\n" +
            "            \"flag\": false,\n" +
            "            \"menuIds\": null,\n" +
            "            \"deptIds\": null,\n" +
            "            \"admin\": false\n" +
            "        }\n" +
            "    ],\n" +
            "    \"posts\": null\n" +
            "}";
    @GetMapping("/user/")
    public Result getQuanXian(){
        String jsonStr = JSONUtil.toJsonStr(s);
        return Result.success(jsonStr);

    }
@GetMapping("/user/listselect")
public ResponseEntity<Map<String, Object>> listselect(@RequestParam(required = false) Integer deptId) {
    List<UserDTO> userList = userService.getUserListByDept(deptId);

    Map<String, Object> response = new HashMap<>();
    response.put("msg", "操作成功");
    response.put("code", 200);
    response.put("data", userList);

    return ResponseEntity.ok(response);
}



//    /**   查询角色  */
//    @GetMapping("/roles")
//    public ResponseEntity<List<RoleResponse>> getAllRolesForUser() {
//        List<RoleResponse> roles = roleService.getAllRolesForUser();
//        return ResponseEntity.ok(roles);
//    }
}

